package com.bocai.kelala.ui.travel.main.act;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelala.App;
import com.bocai.kelala.R;
import com.bocai.kelala.base.BaseActivity;
import com.bocai.kelala.bean.MyAddressBean;
import com.bocai.kelala.ui.travel.main.contract.ModifyAddressContract;
import com.bocai.kelala.ui.travel.main.module.ModifyAddressModule;
import com.bocai.kelala.ui.travel.main.presenter.ModifyAddressPresenter;
import com.bocai.kelala.widget.DetailAddressDialog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/8/24.
 */

public class ModifyAddressActivity extends BaseActivity implements ModifyAddressContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.ll_delete)
    LinearLayout llDelete;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_start_address)
    TextView tvStartAddress;
    @BindView(R.id.ll_start_address)
    LinearLayout llStartAddress;
    @BindView(R.id.tv_end_address)
    TextView tvEndAddress;
    @BindView(R.id.ll_end_address)
    LinearLayout llEndAddress;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    @Inject
    ModifyAddressPresenter presenter;

    private String orderId;
    private String startName,endName;
    private DetailAddressDialog detailAddressDialog;
    private DetailAddressDialog detailAddressDialog1;
    private String priceId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_address;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new ModifyAddressModule(this)).inject(this);
        initToolbar("修改线路");
        initBack(imgBack,llBack);
        detailAddressDialog = new DetailAddressDialog(this);
        detailAddressDialog1 = new DetailAddressDialog(this);
    }

    @Override
    public void initView() {
        orderId = getIntent().getStringExtra("orderId");
        startName = getIntent().getStringExtra("startName");
        endName = getIntent().getStringExtra("endName");
        tvStartAddress.setText(startName);
        tvEndAddress.setText(endName);
        presenter.getAddressList(orderId);
    }

    @OnClick({R.id.ll_start_address,R.id.ll_end_address,R.id.btn_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_start_address:
                detailAddressDialog.show();
                break;
            case R.id.ll_end_address:
                detailAddressDialog1.show();
                break;
            case R.id.btn_confirm:
                if (TextUtils.isEmpty(priceId)){
                    showErrorTip("请选择目的地的详细地址");
                }else {
                    presenter.modifyOrderAddress(orderId,priceId);
                }
                break;
        }
    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
    }

    @Override
    public void getAddressList(final List<MyAddressBean> list) {
        final List<MyAddressBean> addressBeanList = new ArrayList<>();
        for (MyAddressBean bean :list){
            if (bean.getLevel() == 1){
                addressBeanList.add(bean);
            }
        }



        detailAddressDialog.builder().setList(addressBeanList, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tvStartAddress.setText(addressBeanList.get(position).getFullName());


                final List<MyAddressBean> addressBeanList1 = new ArrayList<>();
                for (MyAddressBean bean :list){
                    if (bean.getParentId().equals(addressBeanList.get(position).getId())){
                        addressBeanList1.add(bean);
                    }
                }
                priceId = "";
                tvEndAddress.setText("");
                detailAddressDialog1.builder().setList(addressBeanList1, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        tvEndAddress.setText(addressBeanList1.get(position).getFullName());
                        detailAddressDialog1.dismiss();
                        priceId = addressBeanList1.get(position).getId();
                    }
                });
                detailAddressDialog.dismiss();
            }
        });



        final List<MyAddressBean> addressBeanList2 = new ArrayList<>();
        String id = "";
        for (MyAddressBean bean :list){
            if (bean.getFullName().equals(startName)||startName.contains(bean.getFullName())){
                if (bean.getLevel() == 1) {
                    id = bean.getId();
                }
            }
        }
        for (MyAddressBean bean :list){
            if (bean.getParentId().equals(id)){
                addressBeanList2.add(bean);
            }
        }

        detailAddressDialog1.builder().setList(addressBeanList2, new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tvEndAddress.setText(addressBeanList2.get(position).getFullName());
                detailAddressDialog1.dismiss();
                priceId = addressBeanList2.get(position).getId();
            }
        });
    }

    @Override
    public void modifySuccess() {
        showErrorTip("线路已修改，等待司机反馈");
        setResult(234);
        finish();
    }
}
